/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.classic.common.simple;

import ohos.agp.animation.AnimatorValue;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 列表适配
 *
 * @since 2021-05-13
 */
public abstract class LoadingIndicatorView extends Component {
    private static final String TAG = "AVLoadingIndicatorView";

    private static final int mMinWidth = 200;

    private static final int mMaxWidth = 400;

    private static final int mMinHeight = 200;

    private static  final int mMaxHeight = 400;
    private static final int num231 = 231;
    private static final int num87 = 87;
    private static final int num100 = 100;
    private Paint mPaint = new Paint();

    private Rect drawBounds = new Rect();

    private boolean isHasAnimators;
    private HashMap<AnimatorValue, AnimatorValue.ValueUpdateListener> mUpdateListeners = new HashMap<>();

    private ArrayList<AnimatorValue> mAnimators;

    /**
     * View
     *
     * @param context context
     */
    public LoadingIndicatorView(Context context) {
        super(context);
    }

    /**
     * View
     *
     * @param context context
     * @param attrs attrs
     */
    public LoadingIndicatorView(Context context, AttrSet attrs) {
        super(context, attrs);
    }

    /**
     * setPaint()
     */
    public void setPaint() {
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPaint.setAntiAlias(true);
        mPaint.setColor(new Color(new RgbColor(num231, num87, num100).asArgbInt()));
    }

    public Paint getPaint() {
        return mPaint;
    }

    /**
     * setPaint()
     */

    public void setBound() {
        int minValue = Math.min(getWidth(), getHeight());
        if (minValue >= getmMaxHeight() || minValue >= getmMaxWidth()) {
            setWidth(getmMaxWidth());
            setHeight(getmMaxHeight());
        } else if (minValue < getmMinHeight() || minValue < getmMinWidth()) {
            setWidth(getmMinWidth());
            setHeight(getmMinHeight());
        } else {
            setHeight(minValue);
            setWidth(minValue);
        }
    }

    public Rect getBound() {
        return drawBounds;
    }

    /**
     * draw
     *
     * @param canvas Canvas
     * @param paint paint
     */
    public abstract void draw(Canvas canvas, Paint paint);

    /**
     * onCreateAnimators
     *
     * @return ArrayList
     */
    public abstract ArrayList<AnimatorValue> onCreateAnimators();

    /**
     * addUpdateListener
     *
     * @param animator animator
     * @param updateListener updateListener
     */
    public void addUpdateListener(AnimatorValue animator, AnimatorValue.ValueUpdateListener updateListener) {
        mUpdateListeners.put(animator, updateListener);
    }

    private void startAnimators() {
        for (int i1 = 0; i1 < mAnimators.size(); i1++) {
            AnimatorValue animator = mAnimators.get(i1);
            AnimatorValue.ValueUpdateListener updateListener = mUpdateListeners.get(animator);
            if (updateListener != null) {
                animator.setValueUpdateListener(updateListener);
            }
            animator.start();
        }
    }

    private void stopAnimators() {
        if (mAnimators != null) {
            for (AnimatorValue animator : mAnimators) {
                if (animator != null) {
                    animator.end();
                }
            }
        }
    }

    private void ensureAnimators() {
        if (!isHasAnimators) {
            mAnimators = onCreateAnimators();
            isHasAnimators = true;
        }
    }

    /**
     * stop()
     */
    public void stop() {
        stopAnimators();
    }

    /**
     * start()
     */
    public void start() {
        ensureAnimators();
        startAnimators();
    }

    @Override
    public void setVisibility(int vv) {
        if (getVisibility() != vv) {
            super.setVisibility(vv);
            if (vv == HIDE || vv == INVISIBLE) {
                stopAnimators();
            } else {
                startAnimators();
            }
        }
    }

    /**
     * show
     */
    public void show() {
        setVisibility(VISIBLE);
    }

    /**
     * getmMinWidth
     *
     * @return mMinWidth mMinWidth
     */

    public int getmMinWidth() {
        return mMinWidth;
    }

    /**
     * getmMaxWidth
     *
     * @return mMaxWidth
     */
    public int getmMaxWidth() {
        return mMaxWidth;
    }

    /**
     * getmMinHeight
     *
     * @return mMinHeight
     */
    public int getmMinHeight() {
        return mMinHeight;
    }

    /**
     * getmMaxHeight
     *
     * @return mMaxHeight
     */

    public int getmMaxHeight() {
        return mMaxHeight;
    }
}
